package com.wetao.education.module_reader.presenter;

import android.content.Context;

import com.google.gson.Gson;
import com.wetao.education.module_reader.R;
import com.wetao.education.module_reader.contract.ReadPushCon;
import com.wetao.education.module_reader.contract.ReadPushDetailCon;
import com.wetao.lib_common.base.Base2Response;
import com.wetao.lib_common.base.BaseResponse;
import com.wetao.lib_common.bean.response.ReadPushFirstInfo;
import com.wetao.lib_common.bean.response.ReadPushListInfo;
import com.wetao.lib_common.net.RetryWithDelay;
import com.wetao.lib_common.net.RxUtils;
import com.wetao.lib_common.net.ServerUtils;
import com.wetao.lib_common.net.callback.RequestCallback;
import com.wetao.lib_common.net.callback.RxErrorHandler;
import com.wetao.lib_common.utils.ToastUtil;

import java.util.HashMap;

import okhttp3.RequestBody;

/**
 * author: zhouyh
 * created on: 2020/11/11 4:20 PM
 * description:
 */
public class ReadPushDetailPre extends ReadPushDetailCon.Presenter {

    private Context mContext;

    public ReadPushDetailPre(Context mContext) {
        this.mContext = mContext;
    }



    @Override
    public void beginRead(HashMap<String, String> map, boolean isDialog, boolean isCancelable) {
        if (getView()==null)return;
        String json =(new Gson()).toJson(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().beginRead(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseResponse>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse>(mContext, RxErrorHandler.getInstance(),isDialog,isCancelable) {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        super.onNext(baseResponse);
                        if (getView()!=null){
                            if(baseResponse.getCode() == 0){
                                getView().beginReadResult(baseResponse);
                            }else{
                                getView().beginReadFail();
                            }
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ToastUtil.initToast(mContext.getString(R.string.network_disconnect));
                        getView().beginReadFail();
                    }
                });
    }

    @Override
    public void endRead(HashMap<String, String> map, boolean isDialog, boolean isCancelable, final boolean isExit) {
        if (getView()==null)return;
        String json =(new Gson()).toJson(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().endRead(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseResponse>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse>(mContext, RxErrorHandler.getInstance(),isDialog,isCancelable) {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().endReadResult(baseResponse, isExit);
                        }else{
                            ToastUtil.initToast(baseResponse.getMsg());
                            getView().endReadFail(isExit);
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ToastUtil.initToast(mContext.getString(R.string.network_disconnect));
                        getView().endReadFail(isExit);
                    }
                });
    }

    @Override
    public void getFirstRead(boolean isDialog, boolean isCancelable) {
        if (getView()==null)return;
        ServerUtils.getCommonApi().getFirstRead()
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<ReadPushFirstInfo>bindToLifecycle(getView()))
                .compose(RxUtils.<ReadPushFirstInfo>getSchedulerTransformer())
                .subscribe(new RequestCallback<ReadPushFirstInfo>(mContext, RxErrorHandler.getInstance(),isDialog,isCancelable) {
                    @Override
                    public void onNext(ReadPushFirstInfo baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getFirstReadResult(baseResponse);
                        }else{
                            ToastUtil.initToast(baseResponse.getMsg());
                            getView().getFirstReadFail();
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ToastUtil.initToast(mContext.getString(R.string.network_disconnect));
                        getView().getFirstReadFail();
                    }
                });
    }
}
